package com.interview.multi.algrithm;

import java.util.concurrent.*;

public class ThreadsCount {

    public static void main(String[] args) {
        int a = 1;
        int b = 2;
        int c = 98;
        int d = 1;
        int sum = 0;
        ThreadA threadA = new ThreadA(a, b);
        ThreadB threadB = new ThreadB(c, d);
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        Future<Integer> first = executorService.submit(threadA);
        Future<Integer> two = executorService.submit(threadB);
        try {
            Integer aa = first.get();
            Integer bb = two.get();
            sum = aa + bb;
            System.out.println(sum);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }
    }

}

class ThreadA implements Callable<Integer> {

    int a = 0;
    int b = 0;

    public ThreadA(int a, int b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public Integer call() throws Exception {
        return a * b;
    }

}

class ThreadB implements Callable<Integer> {

    // 处理d=0的异常
    int c = 0;
    int d = 0;

    public ThreadB(int c, int d) {
        this.c = c;
        this.d = d;
    }

    @Override
    public Integer call() throws Exception {
        return c / d;
    }

}
